var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');

var app = getApp();
Page({
  data: {
    username: '',
    password: '',
    code: '',
    loginErrorCount: 0
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function() {

  },
  onShow: function() {
    // 页面显示
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭

  },
  accountLogin: function() {
    var that = this;

    if (this.data.password.length < 1 || this.data.username.length < 1) {
      wx.showModal({
        title: '错误信息',
        content: '请输入用户名和密码',
        showCancel: false
      });
      return false;
    }

    wx.request({
      url: api.AuthLoginByAccount,
      data: {
        username: that.data.username,
        password: that.data.password
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
  
        if (res.data.errno == 0) {
          console.log(res)
          that.setData({
            loginErrorCount: 0
          });
          app.globalData.hasLogin = true;
          wx.setStorageSync('roldId', 1)
          console.log(wx.getStorageSync('roldId'))
          app.globalData.bread = res.data.data.bread
          console.log(app.globalData.bread)
          app.globalData.user=res.data.data.user
          console.log(app.globalData.user)
          console.log(res.data.data.order)
          app.globalData.order = res.data.data.order
          
          wx.switchTab({
            url: "/pages/ucenter/admin/admin"
          })
        } else {
          that.setData({
            loginErrorCount: that.data.loginErrorCount + 1
          });
          wx.setTabBarItem({
            index: 0,
          })
          console.log(app.tabBar)
          

          app.globalData.hasLogin = false;
         
          console.log(getApp())


          util.showErrorToast('账户登录失败');
        }
      }
    });


  },
  bindUsernameInput: function(e) {

    this.setData({
      username: e.detail.value
    });
  },
  bindPasswordInput: function(e) {

    this.setData({
      password: e.detail.value
    });
  },
  bindCodeInput: function(e) {

    this.setData({
      code: e.detail.value
    });
  },
  clearInput: function(e) {
    switch (e.currentTarget.id) {
      case 'clear-username':
        this.setData({
          username: ''
        });
        break;
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-code':
        this.setData({
          code: ''
        });
        break;
    }
  },
  changeBlob: function(imgPath) {
    wx.chooseMedia({
      success: res => {
        console.log(res)
        // 借鉴：https://blog.csdn.net/qq_36875339/article/details/81086205
        // 简写模式：
        console.log(res.tempFiles)
        // let imgbase64 = 'data:image/png;base64,' + wx.getFileSystemManager().readFileSync(res.tempFiles[0], "base64")
        // 详细模式：
        wx.uploadFile({
          filePath: 'filePath',
          name: 'name',
          url: 'url',
        })
        let imgbase64 = wx.getFileSystemManager().readFile({
          filePath: imgPath, //选择图片返回的相对路径
          encoding: 'base64', //编码格式
          success: res => { //成功的回调
            console.log('data:image/png;base64,' + res.data)
          }
        })
        console.log(imgbase64)
        this.data.imgList.push(imgbase64)
        this.setData(this.data)
      }
    })
  },
})